<template>
  <div>
    <h1>顶部广告列表</h1>
    <el-table :data="items">
      <el-table-column prop="image" label="图片" width="240">
        <template slot-scope="scope">
          <img :src="scope.row.image" alt="" style="height:auto;width:5rem">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="remove(scope.row._id, scope.row.name)" type="text" size="small">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: []
    };
  },

  methods: {
    async fetch() {
      const res = await this.$http.get("rest/ads");
      this.items = res.data;
    },
    async remove(id, name) {
      this.$confirm(`是否移除广告商品："${name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await this.$http.delete(`rest/ads/${id}`).then(() => {
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.fetch();
          });
        })
        .catch(() => {});
    }
  },
  created() {
    this.fetch();
  },
  mounted() {}
};
</script>
<style scoped>
</style>